
#include "VInfra/VDebug.h"
#include "VInfra/VMutex.h"
#include "VInfra/VTime.h"
#include "VInfra/VThread.h"

#include "_test_Veda.h"

using namespace Veda;

namespace VedaTest{

class test_thread1 : public VThread
{
public:
	test_thread1(): VThread("test_thread1", Realtime){num = 927;}
	~test_thread1(){tracef("before ~test_thread1\n");stopThread();waitThread();tracef("after ~test_thread1\n");}
	void ThreadProc()
	{
		int i=0;
		while(loop())
		{
			warnf("in thread1 %d\n",i++);
			VTime::sleep(1);
		}
	}
	void printsomething()
	{
		debugf("in test_thread1 printsomething %d\n",num);
	}
	int num;
};

class test_thread2 : public VThread
{
public:
	test_thread2(): VThread("test_thread2", Normal){num=763;}
	~test_thread2(){tracef("before ~test_thread2\n");stopThread();waitThread();tracef("after ~test_thread2\n");}
	void ThreadProc()
	{
		int i=0;
		while(loop())
		{
			errorf("in thread2 %d\n",i++);
			VTime::sleep(1);
		}
	}
	void printsomething()
	{
		warnf("in test_thread2 printsomething %d\n",num);
	}
	int num;
};
class test_thread3 : public VThread
{
public:
	test_thread3(): VThread("test_thread3", Critical){}
	~test_thread3(){tracef("before ~test_thread3\n");stopThread();waitThread();tracef("after ~test_thread3\n");}
	void ThreadProc()
	{
		int i=0;
		while(loop())
		{
			infof("in thread3 %d\n",i++);
			VTime::sleep(1);
		}
	}
};

void test_onThreadstop(VThread* p)
{
	tracef("on threadstop TID : %d CurTID : %d\n",p->getThreadID(),VThread::getCurrentThreadID());
	((test_thread1*)p)->printsomething();
	p->startThread();
}


void _test_VThread()
{
	debugf("sizeof test_thread1 : %d\n",sizeof(test_thread1));
	test_thread1 a;
	test_thread2 b;
	test_thread3 c;
	test_thread3 d;
	test_thread3 e;

	a.ThreadStop.attach(&test_onThreadstop);
	b.ThreadStop.attach(&b,&test_thread2::printsomething);

	a.startThread();
	b.startThread();
	c.startThread();
	VTime::sleep(5);
}

}